package com.google.code.dweibo.util;

import java.util.ArrayList;
import java.util.List;

import weibo4j.model.User;

import com.google.code.dweibo.core.user.SinaUserActionService;
import com.google.code.dweibo.core.user.SinaUserViewService;

public class GarbageUserFilter {
	private SinaUserActionService actionService;
	private SinaUserViewService viewService;

	public GarbageUserFilter() throws Exception {
		actionService = new SinaUserActionService();
		viewService = new SinaUserViewService();
	}

	public void removeGarbageUsers() {
		List<User> myFollowers = viewService.showFollowers(null);
		List<User> myFriends = viewService.showFriends(null);
		List<User> removeList = new ArrayList<User>();
		for (User user : myFollowers) {
			List<User> friends = viewService.showFriends(user.getId());
			if (!myFriends.contains(user)) {
				if (!checkNest(friends, myFriends)
						&& !checkFriendsByMyFreinds(myFriends, user)) {
					removeList.add(user);
				}

			}
		}

		printFilterList(removeList);
	}

	private boolean checkNest(List<User> a, List<User> b) {
		for (User user : b) {
			if (a.contains(user)) {
				return true;
			}
		}
		return false;
	}

	private boolean checkFriendsByMyFreinds(List<User> userList, User user) {
		for (User friend : userList) {
			List<User> friendList = viewService.showFriends(friend.getId());
			if (friendList.contains(user)) {
				return true;
			}
		}
		return false;
	}

	private void printFilterList(List<User> userList) {
		for (User user : userList) {
			System.out.println(user.getName());
		}
	}

	public static void main(String[] args) throws Exception {
		GarbageUserFilter filter = new GarbageUserFilter();
		filter.removeGarbageUsers();
	}
}
